package com.logicfishsoftware.wolfgo.meta;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.openrdf.model.Graph;
import org.openrdf.model.Resource;
import org.openrdf.model.impl.GraphImpl;
import org.openrdf.model.util.GraphUtil;
import org.openrdf.model.util.GraphUtilException;
import org.openrdf.model.vocabulary.RDF;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.config.RepositoryConfig;
import org.openrdf.repository.config.RepositoryConfigException;
import org.openrdf.repository.config.RepositoryConfigSchema;
import org.openrdf.repository.manager.RepositoryManager;
import org.openrdf.repository.manager.RepositoryProvider;
import org.openrdf.repository.object.ObjectRepository;
import org.openrdf.repository.object.config.ObjectRepositoryFactory;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.RDFParseException;
import org.openrdf.rio.RDFParser;
import org.openrdf.rio.Rio;
import org.openrdf.rio.helpers.StatementCollector;
import org.openrdf.store.blob.BlobStoreFactory;

public class SesameUtils {
	public static ObjectRepository createRepos(String repositoryId, String template) 
	throws RepositoryConfigException, RepositoryException, RDFParseException, RDFHandlerException, GraphUtilException, IOException {
		RepositoryManager manager = RepositoryProvider.getRepositoryManager("target");
		URL url = SesameUtils.class.getClassLoader().getResource(template + ".ttl");
		manager.addRepositoryConfig(createConfig(url));
		Repository repository = manager.getRepository(repositoryId);
		ObjectRepositoryFactory repositoryFactory = new ObjectRepositoryFactory();
		ObjectRepository objectRepository = repositoryFactory.createRepository(repository);
		objectRepository.setCompileRepository(false);
		objectRepository.setBlobStore(BlobStoreFactory.newInstance().openBlobStore(new File("target/blobstore")));
		return objectRepository;
	}
	public static RepositoryConfig createConfig(URL url) throws IOException,
	RDFParseException, RDFHandlerException, GraphUtilException,
	RepositoryConfigException {
		Graph graph = new GraphImpl();
		RDFParser rdfParser = Rio.createParser(RDFFormat.TURTLE);
		rdfParser.setRDFHandler(new StatementCollector(graph));

		String base = new File(".").getAbsoluteFile().toURI().toASCIIString();
		URLConnection con = url.openConnection();
		StringBuilder sb = new StringBuilder();
		for (String mimeType : RDFFormat.TURTLE.getMIMETypes()) {
			if (sb.length() < 1) {
				sb.append(", ");
			}
			sb.append(mimeType);
		}
		con.setRequestProperty("Accept", sb.toString());
		InputStream in = con.getInputStream();
		try {
			rdfParser.parse(in, base);
		} finally {
			in.close();
		}

		Resource node = GraphUtil.getUniqueSubject(graph, RDF.TYPE,
				RepositoryConfigSchema.REPOSITORY);
		RepositoryConfig config = RepositoryConfig.create(graph, node);
		config.validate();
		return config;
	}

	public static ObjectRepository connectRepos(String repositoryId) throws RepositoryConfigException, RepositoryException {
		RepositoryManager manager = RepositoryProvider.getRepositoryManager(".");
		Repository repository = manager.getRepository(repositoryId);
		ObjectRepositoryFactory repositoryFactory = new ObjectRepositoryFactory();
		ObjectRepository objectRepository = repositoryFactory.createRepository(repository);
		return objectRepository;
	}

}
